package com.heima.search.controller;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.search.dtos.SearchDto;
import com.heima.model.search.dtos.WordsDto;
import com.heima.search.service.SearchService;
import com.heima.search.utils.Token;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 嘉德罗斯
 * @date 2021/11/27
 */
@RestController
@RequestMapping("/api/v1")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @PostMapping("/article/search/search")
    public ResponseResult search(@RequestBody SearchDto dto, HttpServletRequest request){
        Integer id = Token.getId(request);
        searchService.save(id,dto.getSearchWords());
        return searchService.search(dto);
    }

    @PostMapping("/history/load")
    public ResponseResult load(HttpServletRequest request){
        Integer id = Token.getId(request);
        return searchService.load(id);
    }

    @PostMapping("/history/del")
    public ResponseResult del(@RequestBody Map map){
        String id = map.get("id") + "";
        return searchService.del(id);
    }

    @PostMapping("/associate/search")
    public ResponseResult associate(@RequestBody WordsDto dto){
        return searchService.associate(dto);
    }

    @PostMapping("/hot_keywords/load")
    public ResponseResult hot(@RequestBody WordsDto dto){
        return searchService.hot(dto);
    }
}
